from unicorn.http.client import UnicornClient, CommunicationError
from unicorn.signal import IsApplicationAllowed
from kidux.apps.im import signal as IMSignal
from kidux.apps.im.config import DEBUG

class IMClient(UnicornClient):
    def __init__(self, plugin):
        super(IMClient, self).__init__("im")
        self.plugin = plugin

    def _dbg(self, *args, **kwargs):
        if DEBUG:
            print "DEBUG: kidux.apps.im.client:", args, kwargs

    def send(self, *args, **kwargs):
        try:
            return super(IMClient, self).send(*args, **kwargs)
        except CommunicationError, e:
            self._dbg("ERROR ConnectionError:", str(e))

    def setStatus(self, isOnline, accountId=None, contactId=None):
        self._dbg("setStatus", isOnline, accountId, contactId)
        self.send(IMSignal.SetStatus(isOnline, accountId, contactId))

    def setNick(self, nick, accountId=None, contactId=None):
        self._dbg("setNick", nick, accountId, contactId)
        self.send(IMSignal.SetNickName(nick, accountId, contactId))

    def setMessage(self, message, accountId=None, contactId=None):
        self._dbg("setMessage", message, accountId, contactId)
        self.send(IMSignal.SetPersonalMessage(message, accountId, contactId))

    def setAvatar(self, avatarPath, accountId=None, contactId=None):
        self._dbg("setAvatar", avatarPath, accountId, contactId)
        if not avatarPath:
            return
        self.send(IMSignal.SetAvatar(avatarPath, accountId,  contactId))

    def disconnect(self, accountId):
        self._dbg("disconnect")
        self.send(IMSignal.SetStatus(False, accountId))

    def isApplicationAllowed(self):
        self._dbg("isApplicationAllowed")
        return self.sendAndWait(IsApplicationAllowed(self.applicationId))

    def isContactAllowed(self, accountId, contactId):
        self._dbg("isContactAllowed", accountId, contactId)
        return self.sendAndWait(IMSignal.IsContactAllowed(accountId, contactId))

    def isFileTransferAllowed(self, accountId, contactId):
        self._dbg("isFileTransferAllowed", accountId, contactId)
        return self.sendAndWait(IMSignal.IsFileTransferAllowed(accountId, contactId))

    def enterConversation(self, conversationId, accountId=None, contactId=None):
        self._dbg("enterConversation", conversationId, accountId, contactId)
        self.send(IMSignal.EnterConversation(conversationId, accountId, contactId))

    def logConversation(self, conversationId, message, accountId=None, contactId=None):
        self._dbg("logConversation", conversationId, message, accountId, contactId)
        self.send(IMSignal.LogConversation(conversationId, message, accountId, contactId))

    def leaveConversation(self, conversationId, accountId=None, contactId=None):
        self._dbg("leaveConversation", conversationId, accountId, contactId)
        self.send(IMSignal.LeaveConversation(conversationId, accountId, contactId))

    def addContact(self, group="", accountId=None, contactId=None):
        self._dbg("addContact", group, accountId, contactId)
        self.send(IMSignal.AddContact(group, accountId, contactId))

    def removeContact(self, accountId=None, contactId=None):
        self._dbg("removeContact", accountId, contactId)
        self.send(IMSignal.RemoveContact(accountId, contactId))

    def logFileTransfer(self, conversationId, remoteFileName, localPath, accountId=None, contactId=None):
        self._dbg("logFileTransfer", conversationId, remoteFileName, localPath, accountId, contactId)
        self.send(IMSignal.LogFileTransfer(conversationId, remoteFileName, localPath, accountId, contactId))

    def setGroup(self, group, accountId=None, contactId=None):
        self._dbg("setGroup", group, accountId, contactId)
        self.send(IMSignal.SetGroup(group, accountId, contactId))

